/*
 * Copyright (c) 2020, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "iosdrv.h"
#include "console.h"
#include "adc.h"
#include "debug.h"
#include "syslog.h"
#include <string.h>
#include "numberical.h"

static enConsoleResult adc_cmd(int argc, char** argv)
{
	enConsoleResult ret = CE_OK;

	if (argc == 4)
	{
		FD fd = open(argv[1], 0, 0);
		Adc* adc = (Adc*)fd;

		if( is_int(argv[3])){
			int channel = (int)StringToInt(argv[3]);

			if (adc != NULL) {
				if (strcmp(argv[2], "enable")==0) {
					AdcEnable(adc, channel);
				}else if(strcmp(argv[2], "disable") == 0) {
					AdcDisable(adc, channel);
				}else if (strcmp(argv[2], "read")== 0) {
					uint32_t result = AdcReadValue(adc, channel);
					KPrintf("value : %d (DEC) : 0x%X (HEX)\r\n", result, result);
				}
				ret = CE_OK;
			}
			else {
				SYSLOG(LOG_ERR, "no device found!");
			}
		}else{
			ret = CE_PARAM_ERROR;
		}
		close(fd);
	}
	else 
	{
		KPrint("adc [name] [enable|disable|read] [channel]\r\n");
	}

	return ret;
}

BGEIN_DEF_CMD(_adcCmd)
	CMD_DEF_ALISA(adc, adc_cmd, "adc [name] [enable|disable|read] [channel]")
END_DEF_CMD(_adcCmd)

void AdcCreate(Adc* adc, const char* name, AdcOps* ops)
{
    adc->super.popen = NULL;
	adc->super.pioctl = ops->adc_ioctl;
	adc->super.pread = ops->adc_read;
	adc->super.pwrite = NULL;
	adc->super.pclose = NULL;
	adc->Protected.ops = ops;

    IosDrvInstall(&adc->super, name);

#if defined(BSP_USE_ADC) && defined(COM_USE_ADC_CMD)    
	CONSOLE_REG_CMD(_adcCmd);
#endif 
}

int AdcEnable(FD fd, int channel)
{
	int ret = NG;
	Adc* adc = (Adc*)fd;

	if (adc != NULL) {
		ret = adc->Protected.ops->adc_ioctl((FD)adc, ADC_IO_ENABLE, &channel);
	}

	return (ret);
}

int AdcDisable(FD fd, int channel)
{
	int ret = NG;
	Adc* adc = (Adc*)fd;

	if (adc != NULL) {
		ret = adc->Protected.ops->adc_ioctl((FD)adc, ADC_IO_DISABLE, &channel);
	}
	return (ret);
}

extern uint32_t AdcReadValue(FD fd, int channel)
{
	AdcValue value;
	uint32_t ret = 0;
	Adc* adc = (Adc*)fd;

	value.channel = channel;

	if (adc != NULL) {
		if (adc->Protected.ops->adc_read((FD)adc,   (char*)&value, sizeof(AdcValue)) == 0) {
			ret = (uint32_t)(value.value * adc->Protected.ops->refVol / adc->Protected.ops->convBits);
		}
	}
	
	return ret;
}